package com.appspot.aliga.presentation;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.appspot.aliga.Gameday;
import com.appspot.aliga.R;
import com.appspot.aliga.service.ALigaService;

public class LeagueView extends AligaMainView {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.league_view);
		super.onCreate(savedInstanceState);		

		ALigaService service = new ALigaService(this);
		List<Gameday> list = service.getGameDays(this.getCurrentLeague().getId());
		

		setListAdapter(new ArrayAdapter<Gameday>(this, R.layout.list_item,
				(Gameday[]) list.toArray(new Gameday[list.size()])));

		ListView lv = getListView();
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bundle = new Bundle();
				bundle.putInt(AppKeys.KEY_LEAGUE_ID, getCurrentLeague().getId());
				bundle.putInt(AppKeys.KEY_GAMEDAY, (int)id+1);
				Intent showGamedayView = new Intent(view.getContext(), GamedayView.class);
				showGamedayView.putExtras(bundle);
				startActivityForResult(showGamedayView, 0);
				
//				// When clicked, show a toast with the TextView text
//				Toast.makeText(getApplicationContext(),
//						((TextView) view).getText(), Toast.LENGTH_SHORT).show();
			}
		});	
	}
}